#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include "coro.h"
#include "fs_walk.h"

void find_dir(char *path)
{
	DIR *dir = opendir(path);
	struct dirent *entry;
	struct stat info;
	stat(path, &info);
	if (S_ISDIR(info.st_mode));
    else{
		puts("This is not a directory.\n");
		exit(0);
	}
	while (entry = readdir(dir)) {
		if (strcmp(entry->d_name, ".") == 0)
			continue;
		if (strcmp(entry->d_name, "..") == 0)
			continue;

		char *new_path;
        //bzero(new_path,sizeof(new_path));
		new_path = (char *)malloc(sizeof(char) * 256);
		strcpy(new_path, path);
		strcat(new_path, "/");
		strcat(new_path, entry->d_name);

		if (entry->d_type == DT_DIR) {
			find_dir(new_path);
		}
		if (entry->d_type == DT_REG) {
			coro_yield((unsigned long)new_path);
            //puts(new_path);
		}
		free(new_path);
	}
	closedir(dir);
}

void fs_walk()
{
	find_dir(global_dir);
	coro_yield((unsigned long)NULL);
}
